package com.company.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.ServletActionContext;

import com.company.entity.News;
import com.company.service.NewsService;
import com.sun.org.apache.regexp.internal.recompile;

public class NewsAction extends BaseAction {

	News news;
	NewsService newsService;
	File upload;
	String uploadFileName;
	String uploadContentType;

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	public String execute() throws Exception {
		// TODO Auto-generated method stub
		setNews(newsService.query(Integer.valueOf(ServletActionContext
				.getRequest().getParameter("id").toString())));
		Pattern pattern = Pattern.compile("(\r\n|\r|\n|\n\r)");
		// 正则表达式的匹配一定要是这样，单个替换\r|\n的时候会错误
		Matcher matcher = pattern.matcher(news.getContent());
		String newString = matcher.replaceAll("<br>");
		news.setContent(newString);
		ServletActionContext.getRequest().getSession()
				.setAttribute("new", news);
		return super.execute();
	}

	public String list() {
		ServletActionContext.getRequest().getSession()
				.setAttribute("list", newsService.querys());
		return SUCCESS;
	}

	public String save() throws FileNotFoundException {
		savePic();
		Calendar calendar = Calendar.getInstance();
		news.setYear(String.valueOf(calendar.get(Calendar.YEAR)));
		news.setMd(String.valueOf(calendar.get(Calendar.MONTH)) + "/"
				+ String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
		newsService.save(news);
		list();
		return SUCCESS;
	}

	public String delete() {
		newsService.delete(news);
		list();
		return SUCCESS;
	}

	public String update() {
		if (upload == null) {
			news.setImages(newsService.query(news.getId()).getImages());
		} else {
			savePic();
		}
		newsService.update(news);
		list();
		return SUCCESS;
	}

	public boolean savePic() {
		try {
			if (upload == null) {
				return false;
			}
			InputStream in = new FileInputStream(upload);
			String dir = ServletActionContext.getRequest().getRealPath(
					"/images");
			File fileLocation = new File(dir);
			// 此处也可以在应用根目录手动建立目标上传目录
			if (!fileLocation.exists()) {
				boolean isCreated = fileLocation.mkdir();
				if (!isCreated) {
					// 目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。
					return false;
				}
			}
			uploadFileName = UUID.randomUUID().toString() + "."
					+ uploadContentType.split("/")[1];
			news.setImages(uploadFileName);
			File uploadFile = new File(dir, uploadFileName);
			OutputStream out = new FileOutputStream(uploadFile);
			byte[] buffer = new byte[1024 * 1024];
			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
			return false;
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

}
